package com.customization.assets.formmode.remind;

import com.customization.assets.util.AbstractCronJob;
import com.customization.assets.util.AssetRemind;
import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.soa.workflow.request.RequestService;

import java.util.*;

/**
 * @author	Huangwm
 * @description	针对有责任人的实物资产，如果责任人的状态在人员表中变更为离职，则把责任人改为该部门管理员中的一个，并且通知部门管理员 在使用
 */
public class ResourceStatusChangeRemind extends AbstractCronJob {
    private LogTool log = new LogTool("/log/assetlog/huangwm/ResourceStatusChangeRemind",false);
    private DBTools dbTools = new DBTools();
    private DateTools dateTools = new DateTools();
    @Override
    public void start() throws Exception {
        log.writeLog("检查人员状态开始");
        List<Map<String,String>> swzcList = getSwzcList();
        List<Map<String,String>> swzcBmList = getSwzcBmList();
        RecordSet rs = new RecordSet();
        if(swzcBmList.size()>0) {
            for (int i = 0; i < swzcBmList.size(); i++) {
                String dybm = swzcBmList.get(i).get("dybm");
                String counts = swzcBmList.get(i).get("counts");
                String gly = "";
                String[] bmzcgly ;
                log.writeLog(dybm+">>>"+dbTools.getAssetsAdmin_bm(dybm));
                if(!dybm.equals("1")){
                    bmzcgly = dbTools.getAssetsAdmin_bm(dybm).split(",");
                    //获取的管理员不为空才处理
                    if(bmzcgly.length>0){
                        Random random1 = new Random();
                        int index = random1.nextInt(bmzcgly.length);
                        gly = bmzcgly[index];
                        List<String> ids = new ArrayList<>();
                        for (int j = 0; j < swzcList.size(); j++) {
                            if (dybm.equals(swzcList.get(j).get("dybm"))) {
                                ids.add(swzcList.get(j).get("id"));
                            }
                        }
                        String sql = "update uf_swzczmxx set zrr="+gly+" where id in (" + ids.toString().replace("[", "").replace("]", "") + ") ";
                        log.writeLog("部门id:"+dybm+">>"+sql);
                        boolean flag = rs.execute(sql);
                        if (flag) {
                            try {
                                String title = "实物资产责任人变更提示 " + dateTools.getCurrentDate();
                                Map<String, String> mainData = new HashMap<>();
                                mainData.put("xgzc", ids.toString().replace("[", "").replace("]", ""));
                                mainData.put("txr", "1");
                                mainData.put("tzr", dbTools.getAssetsAdmin_bm(dybm));
                                mainData.put("bz", "系统自动检测到相关资产中的实物资产所关联的责任人状态在关务云中已经变化;<br /> 系统已自动变更实物资产责任人为" + dbTools.getLastname(gly) + "！");
                                mainData.put("xglc", "-1");
                                mainData.put("xgbm", dybm);
                                AssetRemind assetRemind = new AssetRemind();
                                RequestService service = new RequestService();
                                String requestid = service.createRequest(assetRemind.setRequestInfo("1", title, mainData));
                                log.writeLog("提醒发送成功;requestid=" + requestid);
                            } catch (Exception e) {
                                e.printStackTrace();
                                log.writeLog("发送提醒异常  " + e.getMessage());
                            }
                        }
                    }
                }else{
                    //部门为拱北海关的单独处理
                }
            }
        }else{
            log.writeLog("ResourceStatusChangeRemind 获取人员清单资产苏剧为空 结束本次提醒");
        }
        log.writeLog("检查人员状态结束");
    }

    //获取符合的资产数据
    public List<Map<String,String>> getSwzcList(){
        List<Map<String,String>> lists = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select zc.id,zc.swzcbh,zc.dybm,zc.zrr from uf_swzczmxx zc left join HrmResource hrm on zc.zrr=hrm.id where status=5";
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("zrr",rs.getString("zrr"));
            data.put("swzcbh",rs.getString("swzcbh"));
            data.put("dybm",rs.getString("dybm"));
            data.put("id",rs.getString("id"));
            lists.add(data);
        }
        return lists;
    }
    //获取符合的资产数据中的部门
    public List<Map<String,String>> getSwzcBmList(){
        List<Map<String,String>> lists = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select dybm,count(zc.id) as counts from uf_swzczmxx zc left join HrmResource hrm on zc.zrr=hrm.id where status=5 group by dybm";
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> data = new HashMap<>();
            data.put("dybm",rs.getString("dybm"));
            data.put("counts",rs.getString("counts"));
            lists.add(data);
        }
        return lists;
    }

}
